<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                  |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                     |
// +------------------------------------------------------------------------+
// | Author: anguolei@clcw.com.cn                                           |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\CarResource;

use AuctionCore\CarResource\Car;

class CarReport {

    private $car_id = null;

    /**
     * 构造
     * @param  int $car_id  车辆id
     * @return array
     */
    public function __construct($car_id) {
        $this->car_id = $car_id;
        $this->car = new Car($car_id);
        $this->report = $this->car->report();
    }

    /**
     * 车辆信息
     * @return Ambigous <multitype:, \Think\mixed>
     */
    public function detail() {
        return $this->car->detail();
    }

    /**
     * 检测报告
     * @return array
     */
    public function report() {
        $report = array();
        $report['header'] = $this->header();
        $report['base_info'] = $this->base_info();
        $report['procedure'] = $this->procedure();
        $report['configuration'] = $this->configuration();
        $report['base_pics'] = $this->report['pictures'];
        $report['perform'] = $this->perform();
        $report['appear_history'] = $this->appear_history();
        $report['frame_history'] = $this->frame_history();

        //可见损伤
        $report['stat']['visible'] = $this->get_dm_stat(); //可见损伤统计
        $report['stat']['degree'] = $this->get_dm_stat('dm_degree'); //损伤程度统计

        $report['light_pics'] = $this->get_dm_pic('2'); //轻微损伤图片
        $report['normal_pics'] = $this->get_dm_pic('3'); //一般损伤图片
        $report['important_pics'] = $this->get_dm_pic('4'); //重要损伤图片

        $report['big_pic_json'] = $this->get_base_pics_json();
        $report['small_pic_json'] = $this->get_base_pics_json('1');
        $report['appear11'] = $this->get_pos_class();
        $report['frame7'] = $this->get_pos_class('frame');
        $arr = $this->get_dm_point_class();
        $report['frame24'] = $arr['frame']; //24处损伤位置，不包括自定义位置
        $report['outside41'] = $arr['outside']; //41处损伤位置，不包括自定义位置
        $report['inside29'] = $arr['inside']; //29处操作位置，不包括自定义位置


        return $report;
    }

    /**
     * 报告标题
     * @return multitype:NULL string unknown Ambigous <>
     */
    public function header() {
        $header = array();
        $header['car_id'] = $this->report['car_base']['car_id'];
        $header['car_no'] = $this->report['car_base']['sno'];
        $arr = \AuctionCore\CarResource\Car::get_pic_by_key($this->report['car_base']['car_id'],'p_left_front45');
        //基本图片
        $header['car_pic'] = $arr['pic_url'];

        $header['share_first_reg_date'] = date("Y年m月",strtotime($this->report['car_base']['first_reg_date']));
        $header['first_reg_date'] = $this->report['car_base']['first_reg_date'];
        $header['pointer_mileage'] = convert_mileage($this->report['car_base']['pointer_mileage']).'万公里';
        $header['plate_number'] = C('PLATE_PREFIX')[$this->report['car_base']['plate_prefix']] . substr($this->report['car_base']['plate_suffix'], 0, 1);
        $header['check_date'] = date('Y-m-d', strtotime($this->report['car_base']['posttime']));
        $header['composite_state'] = $this->report['car_base']['composite_state'];
        $header['acc_level'] = $this->report['car_base']['accident_level'];
        $header['composite_state_desc'] = C('COMPOSITE_STATE_DESC.' . $this->report['car_base']['composite_state']);
        $header['acc_level_desc'] = C('ACC_LEVEL_DESC.' . $this->report['car_base']['accident_level']);
        if(empty($this->report['car_base']['history_state'])){
            $header['history_state'] = '无水泡,无火烧。';
        }else{
            $arr = explode(',', $this->report['car_base']['history_state']);
            $header['history_state'] = C('HISTORY_STATE.' . $arr[0]);
            if ($arr[1]) {
                $header['history_state'] .= "," . C('HISTORY_STATE.' . $arr[1]);
            }
        }
        $a = new \AuctionCore\Other\Address();
        $area = $a->get_province($this->report['car_base']['reg_city']);
        $header['province'] = $area['province'];
        $header['city'] = $area['city_name'];
        $cm = new \AuctionCore\CarResource\CarModel();
        //$brand = $cm->get_model_by_id($this->report['car_base']['brand_id'], 'brand');
        $series = $cm->get_model_by_id($this->report['car_base']['series_id'], 'series');
        //$model = $cm->get_model_by_id($this->report['car_base']['model_id']);
        $header['emission'] =  C('EMISSION')[$this->report['car_base']['emission']];
        $header['transfer_limit'] =  C('TRANSFER_LIMIT')[$this->report['car_base']['transfer_limit']];
        $header['reserve_price'] = format_money($this->report['car_base']['reserve_price']);
        $header['show_reserve_price'] = $this->report['car_base']['show_reserve_price'] ? "保留价：".$header['reserve_price'] : "有保留价";
        $header['car_cc'] = $this->report['car_base']['car_cc'];
        $header['turbo'] = $this->report['car_base']['turbo'];
        $header['gearbox'] = $this->report['car_base']['gearbox'];
        $model_year = !empty($model['year']) ? $model['year'].'款' : "";

        $city_name = $a->get_name_by_code($this->report['car_base']['location_area']);
        $header['model_show'] =  '['.$city_name .'] '. $series['name'] . ' ' . $header['car_cc'] . ($header['turbo'] ? 'T' : 'L') . ' ' . C('GEARBOX.' . $header['gearbox']);
        $header['location_area'] = $city_name;
        //$series['name'] . ' ' . $car_info['car_cc'] . ($car_info['turbo'] ? 'T' : 'L') . ' ' . C('GEARBOX.' . $car_info['gearbox']);
        $header['clcw_app_model_show'] = '['.$city_name.'] '.$series['name']. ' ' . $header['car_cc'] . ($header['turbo'] ? 'T' : 'L') . ' ' . C('GEARBOX.' . $header['gearbox']);
        $header['app_model_show'] = $header['clcw_app_model_show'];

        //$emp_info = \AuctionCore\Account\Back\Employee::user_info_id($this->report['owner']['checker_id']);
        $emp_info = M('employee')->find($this->report['owner']['checker_id']);
        $header['checker'] = mb_substr($emp_info['real_name'],0,3).'**';
        $header['checker_level'] = C('CHECKER_LEVEL.' . $emp_info['checker_level']);
        $header['damage_pics'] = $this->report['car_base']['damage_pics']?json_decode($this->report['car_base']['damage_pics'],true):array();
        $header['vin'] = $this->report['car_base']['vin'];  //增加vin码
        $header['repair_button'] = "查看维修、保养记录"; //增加repair_button 按钮文案
        return $header;
    }

    /**
     * 车辆基本信息
     * @return multitype
     */
    public function base_info() {
        //获取车辆厂商
        $manufacturer = M('car_manufacturer')->alias('cm')->join(C('DB_PREFIX') . "car_brand cb ON(cb.manufacturer_id=cm.manufacturer_id)")
                        ->where("cb.brand_id='" . $this->report['car_base']['brand_id'] . "'")->getField('cm.full_name');
        $city = new \AuctionCore\Other\Address();
        $reg_city = $city->get_name_by_code($this->report['car_base']['reg_city']);
        $old_reg_city = $city->get_name_by_code($this->report['car_base']['old_reg_city']); //原注册市
        $show_from = $this->get_from();  //获取渠道来源信息
        $comefrom = M("car_owner a")
                ->join('INNER JOIN ' . C('DB_PREFIX') . 'cars AS b ON a.owner_id=b.owner_id')
                ->field('comefrom')
                ->where('b.car_id=' . $this->car_id)
                ->find();
        $arr = array(
            'first_reg_date' => array('label' => '初登日期', 'value' => $this->report['car_base']['first_reg_date']),
            'use_properties' => array('label' => '使用性质', 'value' => C('USE_PROPERTIES.' . $this->report['car_base']['use_properties']),'color'=>$this->report['car_base']['use_properties']>1 ? '#fcfdb0' : ''),
            'pointer_mileage' => array('label' => '表显里程(万公里)', 'value' => convert_mileage($this->report['car_base']['pointer_mileage'])),
            'ex_factory_date' => array('label' => '出厂日期', 'value' => $this->report['car_base']['ex_factory_date']),
            'reg_city' => array('label' => '注册地', 'value' => $reg_city),
            'emission' => array('label' => '排放标准', 'value' => C('EMISSION.' . $this->report['car_base']['emission'])),
            'manufacturer' => array('label' => '汽车厂商', 'value' => $manufacturer),
            'car_cc' => array('label' => '排量', 'value' => $this->report['car_base']['car_cc'] . ($this->report['car_base']['turbo'] ? 'T' : 'L')),
            'gearbox' => array('label' => '变速箱形式', 'value' => C('GEARBOX.' . $this->report['car_base']['gearbox'])),
            'car_type' => array('label' => '车辆类型', 'value' => C('CAR_TYPE.' . $this->report['car_base']['car_type'])),
            'drive_method' => array('label' => '驱动方式', 'value' => C('DRIVE_METHOD.' . $this->report['car_base']['drive_method'])),
            'fuel_type' => array('label' => '燃油类型', 'value' => C('FUEL_TYPE.' . $this->report['car_base']['fuel_type'])),
            'car_color' => array('label' => '颜色', 'value' => C('CAR_COLOR.' . $this->report['car_base']['car_color'])),
            'carrying_num' => array('label' => '核定载客数', 'value' => $this->report['car_base']['carrying_num'] . '人'),
            'tyre_type' => array('label' => '轮胎规格', 'value' => $this->report['car_base']['tyre_type']),
            'brand_model' => array('label' => '品牌型号', 'value' => $this->report['car_base']['brand_model']),
            'engine_no' => array('label' => '发动机号', 'value' => $this->report['car_base']['engine_no']),
            'transfer_times' => array('label' => '过户次数', 'value' => $this->report['car_base']['transfer_times']),
            'last_transfer_time'=>array('label' => '最后过户时间','value'=>$this->report['car_base']['last_transfer_time']),  //最后过户时间
            'cur_use_type' => array('label' => '现使用方式', 'value' => C('CUR_USE_TYPE.' . $this->report['car_base']['cur_use_type']),'color'=>$this->report['car_base']['cur_use_type']>1 ? '#fcfdb0' : ''),
            'get_method' => array('label' => '获得方式', 'value' => C('GET_METHOD.' . $this->report['car_base']['get_method'])),
            'is_imported' => array('label' => '是否进口', 'value' => $this->report['car_base']['is_imported']=='3'?'境外自带':C('IS_IMPORTED.' . $this->report['car_base']['is_imported'])),
            'comefrom' => array('label' => '渠道来源', 'value' => $show_from[$comefrom['comefrom']]),
            'plate' => array('label' => '车牌号', 'value' => C('PLATE_PREFIX.' . $this->report['car_base']['plate_prefix']) . $this->report['car_base']['plate_suffix']),
            'vin' => array('label' => '车架号', 'value' => $this->report['car_base']['vin']),
            'old_use_type' => array('label' => '原使用方式', 'value' => C('OLD_USE_TYPE.' . $this->report['car_base']['old_use_type']),'color'=>$this->report['car_base']['old_use_type']>1 ? '#fcfdb0' : ''), //原使用方式
            'old_reg_city'=>array('label' => '原使用方注册地','value'=>$old_reg_city), //原使用方注册市
        );
        if($this->report['car_base']['last_transfer_time'] == '0000-00-00'){
            unset($arr['last_transfer_time']);
        }

        if($this->report['car_base']['transfer_times'] < 1){ //如果过户次数为0,则不显示 最后过户时间，原使用方注册地，原使用方式
            unset($arr['last_transfer_time']);
            unset($arr['old_reg_city']);
            unset($arr['old_use_type']);
        }
        $last_arr = $this->trim_array($arr);
        return $last_arr;
    }

   /**
     * 来拍车报告分享车辆基本信息
     * @return multitype
     */
    public function lpcapp_base_info() {
        //获取车辆厂商
        $manufacturer = M('car_manufacturer')->alias('cm')->join(C('DB_PREFIX') . "car_brand cb ON(cb.manufacturer_id=cm.manufacturer_id)")
            ->where("cb.brand_id='" . $this->report['car_base']['brand_id'] . "'")->getField('cm.full_name');
        $city = new \AuctionCore\Other\Address();
        $reg_city = $city->get_name_by_code($this->report['car_base']['reg_city']);
        $old_reg_city = $city->get_name_by_code($this->report['car_base']['old_reg_city']); //原注册市
        $arr = array(
            'first_reg_date' => array('label' => '初登日期', 'value' => $this->report['car_base']['first_reg_date']),
            'use_properties' => array('label' => '使用性质', 'value' => C('USE_PROPERTIES.' . $this->report['car_base']['use_properties']),'color'=>$this->report['car_base']['use_properties']>1 ? '#fcfdb0' : ''),
            'pointer_mileage' => array('label' => '表显里程(万公里)', 'value' => convert_mileage($this->report['car_base']['pointer_mileage'])),
            'ex_factory_date' => array('label' => '出厂日期', 'value' => $this->report['car_base']['ex_factory_date']),
            'reg_city' => array('label' => '注册地', 'value' => $reg_city),
            'emission' => array('label' => '排放标准', 'value' => C('EMISSION.' . $this->report['car_base']['emission'])),
            'manufacturer' => array('label' => '汽车厂商', 'value' => $manufacturer),
            'car_cc' => array('label' => '排量', 'value' => $this->report['car_base']['car_cc'] . ($this->report['car_base']['turbo'] ? 'T' : 'L')),
            'gearbox' => array('label' => '变速箱形式', 'value' => C('GEARBOX.' . $this->report['car_base']['gearbox'])),
            'car_type' => array('label' => '车辆类型', 'value' => C('CAR_TYPE.' . $this->report['car_base']['car_type'])),
            'drive_method' => array('label' => '驱动方式', 'value' => C('DRIVE_METHOD.' . $this->report['car_base']['drive_method'])),
            'fuel_type' => array('label' => '燃油类型', 'value' => C('FUEL_TYPE.' . $this->report['car_base']['fuel_type'])),
            'car_color' => array('label' => '颜色', 'value' => C('CAR_COLOR.' . $this->report['car_base']['car_color'])),
            'carrying_num' => array('label' => '核定载客数', 'value' => $this->report['car_base']['carrying_num'] . '人'),
            'tyre_type' => array('label' => '轮胎规格', 'value' => $this->report['car_base']['tyre_type']),
            'brand_model' => array('label' => '品牌型号', 'value' => $this->report['car_base']['brand_model']),
            'engine_no' => array('label' => '发动机号', 'value' => $this->report['car_base']['engine_no']),
            'transfer_times' => array('label' => '过户次数', 'value' => $this->report['car_base']['transfer_times']),
            'last_transfer_time'=>array('label' => '最后过户时间','value'=>$this->report['car_base']['last_transfer_time']),  //最后过户时间
            'cur_use_type' => array('label' => '现使用方式', 'value' => C('CUR_USE_TYPE.' . $this->report['car_base']['cur_use_type']),'color'=>$this->report['car_base']['cur_use_type']>1 ? '#fcfdb0' : ''),
            'get_method' => array('label' => '获得方式', 'value' => C('GET_METHOD.' . $this->report['car_base']['get_method'])),
            'is_imported' => array('label' => '是否进口', 'value' => $this->report['car_base']['is_imported']=='3'?'境外自带':C('IS_IMPORTED.' . $this->report['car_base']['is_imported'])),
            'old_reg_city'=>array('label' => '原使用方注册地','value'=>$old_reg_city), //原使用方注册地
            'old_use_type' => array('label' => '原使用方式', 'value' => C('OLD_USE_TYPE.' . $this->report['car_base']['old_use_type']),'color'=>$this->report['car_base']['old_use_type']>1 ? '#fcfdb0' : ''), //原使用方式
        );
        if($this->report['car_base']['last_transfer_time'] == '0000-00-00'){
            unset($arr['last_transfer_time']);
        }

        if($this->report['car_base']['transfer_times'] < 1){ //如果过户次数为0,则不显示 最后过户时间，原使用方注册地，原使用方式
            unset($arr['last_transfer_time']);
            unset($arr['old_reg_city']);
            unset($arr['old_use_type']);
        }
        $last_arr = $this->trim_array($arr);
        return $last_arr;
    }
    /**
     * 去除值为空的元素
     *
     * @param $arr
     * @return mixed
     */
    public function trim_array($arr){
        foreach ($arr as $key => $val){
            if($val['value'] == '' || $val['value'] == null){
                unset($arr[$key]);
            }
        }
        return $arr;
    }


    /**
     * 来拍车app车辆基本信息
     * @return multitype
     */
    public function app_base_info() {
        //获取车辆厂商
        $manufacturer = M('car_manufacturer')->alias('cm')->join(C('DB_PREFIX') . "car_brand cb ON(cb.manufacturer_id=cm.manufacturer_id)")
                        ->where("cb.brand_id='" . $this->report['car_base']['brand_id'] . "'")->getField('cm.full_name');
        $city = new \AuctionCore\Other\Address();
        $reg_city = $city->get_name_by_code($this->report['car_base']['reg_city']);
        $reg_area = $this->report['car_base']['reg_area']?$city->get_name_by_code($this->report['car_base']['reg_area']):'';
        $old_reg_city = $city->get_name_by_code($this->report['car_base']['old_reg_city']); //原注册市
        $arr = array(
            'first_reg_date' => array('label' => '初登日期', 'value' => $this->report['car_base']['first_reg_date'], 'style' => 0),
            'ex_factory_date' => array('label' => '出厂日期', 'value' => $this->report['car_base']['ex_factory_date'], 'style' => 0),
            'use_properties' => array('label' => '使用性质', 'value' => C('USE_PROPERTIES.' . $this->report['car_base']['use_properties']), 'style' => 0,'color'=>$this->report['car_base']['use_properties']>1?0xfffcfdb0:0),
            'pointer_mileage' => array('label' => '表显里程(万公里)', 'value' => convert_mileage($this->report['car_base']['pointer_mileage']), 'style' => 0),
            'reg_city' => array('label' => '注册地', 'value' => $reg_city .' '.$reg_area, 'style' => 0),
            'emission' => array('label' => '排放标准', 'value' => C('EMISSION.' . $this->report['car_base']['emission']), 'style' => 0),
            'manufacturer' => array('label' => '汽车厂商', 'value' => $manufacturer, 'style' => 0),
            'car_cc' => array('label' => '排量', 'value' => $this->report['car_base']['car_cc'] . ($this->report['car_base']['turbo'] ? 'T' : 'L'), 'style' => 0),
            'gearbox' => array('label' => '变速箱形式', 'value' => C('GEARBOX.' . $this->report['car_base']['gearbox']), 'style' => 0),
            'car_type' => array('label' => '车辆类型', 'value' => C('CAR_TYPE.' . $this->report['car_base']['car_type']), 'style' => 0),
            'drive_method' => array('label' => '驱动方式', 'value' => C('DRIVE_METHOD.' . $this->report['car_base']['drive_method']), 'style' => 0),
            'fuel_type' => array('label' => '燃油类型', 'value' => C('FUEL_TYPE.' . $this->report['car_base']['fuel_type']), 'style' => 0),
            'car_color' => array('label' => '颜色', 'value' => C('CAR_COLOR.' . $this->report['car_base']['car_color']), 'style' => 0),
            'carrying_num' => array('label' => '核定载客数', 'value' => $this->report['car_base']['carrying_num'] . '人', 'style' => 0),
            'tyre_type' => array('label' => '轮胎规格', 'value' => $this->report['car_base']['tyre_type'], 'style' => 0),
            'brand_model' => array('label' => '品牌型号', 'value' => $this->report['car_base']['brand_model'], 'style' => 0),
            'engine_no' => array('label' => '发动机号', 'value' => $this->report['car_base']['engine_no'], 'style' => 0),
            'transfer_times' => array('label' => '过户次数', 'value' => $this->report['car_base']['transfer_times'], 'style' => 0),
            'last_transfer_time'=>array('label' => '最后过户时间','value'=>$this->report['car_base']['last_transfer_time'],'style' => 0),  //最后过户时间
            'cur_use_type' => array('label' => '现使用方式', 'value' => C('CUR_USE_TYPE.' . $this->report['car_base']['cur_use_type']), 'style' => 0,'color'=>$this->report['car_base']['cur_use_type']>1?0xfffcfdb0:0),
            'get_method' => array('label' => '获得方式', 'value' => C('GET_METHOD.' . $this->report['car_base']['get_method']), 'style' => 0),
            'is_imported' => array('label' => '是否进口', 'value' => $this->report['car_base']['is_imported']=='3'?'境外自带':C('IS_IMPORTED.' . $this->report['car_base']['is_imported'])),
            'old_reg_city'=>array('label' => '原使用方注册地','value'=>$old_reg_city,'style' => 0), //原使用方注册地
             'old_use_type' => array('label' => '原使用方式', 'value' => C('OLD_USE_TYPE.' . $this->report['car_base']['old_use_type']),'style' => 0), //原使用方式
        );
        if($this->report['car_base']['last_transfer_time'] == '0000-00-00'){
            unset($arr['last_transfer_time']);
        }

        if($this->report['car_base']['transfer_times'] < 1){ //如果过户次数为0,则不显示 最后过户时间，原使用方注册地，原使用方式
            unset($arr['last_transfer_time']);
            unset($arr['old_reg_city']);
            unset($arr['old_use_type']);
        }
        $last_arr = $this->trim_array($arr);
        return $last_arr;
    }

    /**
     * 车辆手续信息
     * @return multitype
     */
    public function procedure() {
        $com_insure_city = M('addr_area')->where("city_code='" . $this->report['procedure']['com_insure_city'] . "'")->getField('city_name');
        $arr = explode(',', $this->report['procedure']['com_insure_expiry_remark']);
        if (in_array('2', $arr)) {
            $flag = 1;
        }elseif(in_array('1',$arr)){
            $flag_expiry_remark = 1;
        }
        $arr_city = explode(',', $this->report['procedure']['com_insure_city_remark']);
        if(in_array('1',$arr_city)){
            $flag_city_remark = 1;
        }

        $reg_cert_remark = $this->get_license_and_remark($this->report['procedure']['is_reg_cert'],$this->report['procedure']['reg_cert_in_charge']);
        $travel_license_remark = $this->get_license_and_remark($this->report['procedure']['is_travel_license'],$this->report['procedure']['travel_license_in_charge']);
        $procedure_info = array(
            'is_reg_cert' => array('label' => '登记证', 'value' => $reg_cert_remark),
            'is_travel_license' => array('label' => '行驶证', 'value' => $travel_license_remark),
            'verify_expiry' => array('label' => '年检有效期', 'value' => date('Y-m', strtotime($this->report['procedure']['verify_expiry']))),
            'invoice' => array('label' => '发票', 'value' => ($this->report['procedure']['invoice'] > 0 ? '有' : '无')),
            'com_insure_expiry' => array('label' => '交强险有效期', 'value' => $flag ? '未见' : ($flag_expiry_remark ? $this->report['procedure']['com_insure_expiry'].'('.C('COM_INSURE_EXPIRY_REMARK')[1].')': $this->report['procedure']['com_insure_expiry'])),
            'com_insure_city' => array('label' => '交强险所在地', 'value' => $flag ? '未见' : ($flag_city_remark ? $com_insure_city.'('.C('COM_INSURE_CITY_REMARK')[1].')': $com_insure_city)),
            'car_tax_expiry' => array('label' => '车船税', 'value' => $this->report['procedure']['car_tax_expiry_remark'] ? '未见' : ($this->report['procedure']['car_tax_expiry'] ? date('Y', strtotime($this->report['procedure']['car_tax_expiry'])): '无')),
            'pur_tax_cert' => array('label' => '购置税证', 'value' => ($this->report['procedure']['pur_tax_cert'] == 1 ? '有（征税）' : ($this->report['procedure']['pur_tax_cert'] == 3 ? '无' : '有（免税）'))),
            'maintain_man' => array('label' => '保养手册', 'value' => C('MAINTAIN_MAN.' . $this->report['procedure']['maintain_man'])),
            'nameplate' => array('label' => '车身铭牌', 'value' => ($this->report['procedure']['nameplate']==1 ? '有' : '无')),
            'ill_deduc_point' => array('label' => '违章记录', 'value' => (($this->report['procedure']['ill_deduc_point']>0 || $this->report['procedure']['forfeit'] > 0) ? '有' : '无')),
            'appear_change' => array('label' => '外观改装', 'value' => ($this->report['procedure']['appear_change'] == '2' ? '实车与行驶证照片不相符' : '无')), //进口车展示此项
            'commodity_inspection_list' => array('label' => '商检单', 'value' => ($this->report['procedure']['commodity_inspection_list'] == '1' ? '有' : '无')),
            'import_customs_list' => array('label' => '报关单', 'value' => ($this->report['procedure']['import_customs_list'] == '1' ? '有' : '无')),
            'bak_key' => array('label' => '备用钥匙', 'value' => ($this->report['procedure']['carrying_num'] == '1' ? '有' : '无')),
            'transfer_ticket' => array('label' => '过户票', 'value' => ($this->report['procedure']['transfer_ticket'] == '1' ? '有' : '无')),
        );

        if ($this->report['car_base']['is_imported'] == 1) {//进口车不展示车身铭牌和外观改饰
            unset($procedure_info['nameplate']);
        }
        if($this->report['car_base']['transfer_times'] < 1){ //如果过户次数为0,则不显示 过户票
            unset($procedure_info['transfer_ticket']);
        }
        return $procedure_info;
    }
    /* 获取证件有无及备注信息
    *
    * @param $license
    * @param $remark
    * @return string
    */
        public function get_license_and_remark($license, $remark){
            $result = '';
            $result .= ($license == '1' ? '有' : '无');
            if($license != '1'){
                if($remark == 1){
                    $result .=' ( ';
                    $result .= C('LICENSE_IN_CHARGE.'.$remark);
                    $result .=' )';
                    return $result;
                }elseif ($remark == 2){
                    $result .=' ( ';
                    $result .= C('LICENSE_IN_CHARGE.'.$remark);
                    $result .=' )';

                    return $result;
                }
            }
            return $result;
        }

    /**
     * 车辆配置信息
     * @return multitype:multitype:string  multitype:string mixed
     */
    public function configuration() {
        return array(
            'abs' => array('label' => 'ABS', 'value' => C('ABS.' . $this->report['config']['abs'])),
            'air_sac' => array('label' => '气囊', 'value' => $this->report['config']['air_sac'] . '个'),
            'turn_helper' => array('label' => '转向助力', 'value' => C('TURN_HELPER.' . $this->report['config']['turn_helper'])),
            'ele_car_window' => array('label' => '电动车窗', 'value' => C('ELE_CAR_WINDOW.' . $this->report['config']['ele_car_window'])),
            'air_window' => array('label' => '天窗', 'value' => C('AIR_WINDOW.' . $this->report['config']['air_window'])),
            'mirror' => array('label' => '车外后视镜', 'value' => C('MIRROR.' . $this->report['config']['mirror'])),
            'seat_texture' => array('label' => '座椅材质', 'value' => C('SEAT_TEXTURE.' . $this->report['config']['seat_texture'])),
            'seat_adjust' => array('label' => '座椅调节', 'value' => C('SEAT_ADJUST.' . $this->report['config']['seat_adjust'])),
            'seat_func' => array('label' => '座椅功能', 'value' => C('SEAT_FUNC.' . $this->report['config']['seat_func'])),
            'air_cond' => array('label' => '空调', 'value' => C('AIR_COND.' . $this->report['config']['air_cond'])),
            'audio' => array('label' => '音响设备', 'value' => C('AUDIO.' . $this->report['config']['audio'])),
            'dvd' => array('label' => 'DVD', 'value' => C('DVD.' . $this->report['config']['dvd'])),
            'navi' => array('label' => '导航', 'value' => C('NAVI.' . $this->report['config']['navi'])),
            'fixed_cruise' => array('label' => '定速巡航', 'value' => C('FIXED_CRUISE.' . $this->report['config']['fixed_cruise'])),
            'reverse_radar' => array('label' => '倒车雷达', 'value' => C('REVERSE_RADAR.' . $this->report['config']['reverse_radar'])),
            'tire_hub_texture' => array('label' => '轮毂材料', 'value' => C('TIRE_HUB_TEXTURE.' . $this->report['config']['tire_hub_texture'])),
            'inte_key' => array('label' => '智能钥匙', 'value' => C('INTE_KEY.' . $this->report['config']['inte_key'])),
            'reverse_video' => array('label' => '倒车影像', 'value' => C('REVERSE_VIDEO.' . $this->report['config']['reverse_video'])),
        );
    }

    /**
     * 车辆损伤照片
     */
    public function damage_pics(){
        $detail = $this->detail();
        $damage_pics = json_decode($detail['damage_pics'],true);
        return $damage_pics;
    }

    /**
     * 工况及附件说明
     * @return array
     */
    public function perform() {
        return array(
            'starter' => array('label' => '起动机', 'value' => $this->get_the_word('STARTER', $this->report['perform']['starter'])),
            'steering_engine' => array('label' => '转向机', 'value' => $this->get_the_word('STEERING_ENGINE', $this->report['perform']['steering_engine'])), //多选
            'engine' => array('label' => '发动机', 'value' => $this->get_the_word('ENGINE', $this->report['perform']['engine'])), //多选
            'ele_sys' => array('label' => '电器故障', 'value' => $this->get_the_word('ELE_SYS', $this->report['perform']['ele_sys'])), //多选
            'engine_box' => array('label' => '变速箱', 'value' => $this->get_the_word('GEAR_BOX_DAMAGE', $this->report['perform']['engine_box'])), //多选
            'bak_tire' => array('label' => '备胎状态', 'value' => ($this->report['perform']['bak_tire'] == '1' ? '有' : '无')), //单选
            'tool_state' => array('label' => '工具状态', 'value' => $this->get_the_word('TOOL_STATE', $this->report['perform']['tool_state'])), //多选
            'other_info' => array('label' => '车辆补充说明', 'value' => $this->report['perform']['other_info'] ? $this->report['perform']['other_info'] : "无"), //单选
        );
    }

    /**
     * 获得工况检测
     */
    public function get_app_perform($car_id='') { //参数暂不能去掉，旧版本APP还在用
        //$pf = M('CarPerform')->where(array('car_id' => $car_id))->find();
        $pf = $this->report['perform'];
        $tool_state_val = $this->get_the_word('TOOL_STATE', $pf['tool_state']);
        return array(
            'starter' => array('label' => '起动机', 'value' => $this->get_the_word('STARTER', $pf['starter']), 'style' => $this->get_perform_status($pf['starter'])),
            'steering_engine' => array('label' => '转向机', 'value' => $this->get_the_word('STEERING_ENGINE', $pf['steering_engine']), 'style' => $this->get_perform_status($pf['steering_engine'])), //多选
            'engine' => array('label' => '发动机', 'value' => $this->get_the_word('ENGINE', $pf['engine']), 'style' => $this->get_perform_status($pf['engine'])), //多选
            'engine_box' => array('label' => '变速箱', 'value' => $this->get_the_word('GEAR_BOX_DAMAGE', $pf['engine_box']), 'style' => $this->get_perform_status($pf['engine_box'])), //多选
            'tool_state' => array('label' => '工具状态', 'value' => $tool_state_val, 'style' => ($pf['tool_state'] == '4' ? '0' : '1')), //多选
            'ele_sys' => array('label' => '电器故障', 'value' => $this->get_the_word('ELE_SYS', $this->report['perform']['ele_sys']), 'style' => $this->get_perform_status($pf['ele_sys'])), //多选
            'bak_tire' => array('label' => '备胎状态', 'value' => ($pf['bak_tire'] == '1' ? '有' : '无'), 'style' => $this->get_perform_status($pf['bak_tire'])), //单选
            'other_info' => array('label' => '车辆补充说明', 'value' => $this->report['perform']['other_info'], 'style' => '0'), //单选
        );
    }

    /**
     * 获得工况状态
     * @param type $data_src
     * @return int
     */
    private function get_perform_status($data_src) {
        $arr = explode(',', $data_src);
        $status = '';
        if ($arr[0] == 1) {  //无损伤
            $status = 0;
        } else {
            $status = 1;    //有损伤
        }
        return $status;
    }

    /**
     * 外观复修历史
     * @return array
     */
    public function appear_history() {
        return array(
            'l_f_door' => array('label' => '左前门', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_f_door'])),
            'r_f_door' => array('label' => '右前门', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_f_door'])),
            'l_b_door' => array('label' => '左后门', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_b_door'])),
            'r_b_door' => array('label' => '右后门', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_b_door'])),
            'l_f_wing_board' => array('label' => '左前翼子板', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_f_wing_board'])),
            'r_f_wing_board' => array('label' => '右前翼子板', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_f_wing_board'])),
            'l_b_wing_board' => array('label' => '左后翼子板', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_b_wing_board'])),
            'r_b_wing_board' => array('label' => '右后翼子板', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_b_wing_board'])),
            'engine_cover' => array('label' => '发动机舱盖', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['engine_cover'])),
            'car_top' => array('label' => '车顶', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['car_top'])),
            'trunk_cover' => array('label' => '后备箱盖', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['trunk_cover'])),
        );
    }

    /**
     * 外观复修历史   客户端v1.1.1接口用
     * @return array
     */
    public function appear_history_v100() {
        return array(
            '0' => array('label' => '左前门', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_f_door'])),
            '1' => array('label' => '右前门', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_f_door'])),
            '2' => array('label' => '左后门', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_b_door'])),
            '3' => array('label' => '右后门', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_b_door'])),
            '4' => array('label' => '左前翼子板', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_f_wing_board'])),
            '5' => array('label' => '右前翼子板', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_f_wing_board'])),
            '6' => array('label' => '左后翼子板', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_b_wing_board'])),
            '7' => array('label' => '右后翼子板', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_b_wing_board'])),
            '8' => array('label' => '发动机舱盖', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['engine_cover'])),
            '9' => array('label' => '车顶', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['car_top'])),
            '10' => array('label' => '后备箱盖', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['trunk_cover'])),
        );
    }

    /**
     * 骨架修复历史
     * @return array
     */
    public function frame_history() {
        return array(
            'l_a_bar' => array('label' => '左A柱外侧', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_a_bar'])),
            'l_b_bar' => array('label' => '左B柱外侧', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_b_bar'])),
            'l_c_bar' => array('label' => '左C柱外侧', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_c_bar'])),
            'r_c_bar' => array('label' => '右C柱外侧', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_c_bar'])),
            'r_b_bar' => array('label' => '右B柱外侧', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_b_bar'])),
            'r_a_bar' => array('label' => '右A柱外侧', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_a_bar'])),
            'cover_inner_edge' => array('label' => '机盖内缘', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['cover_inner_edge'])),
        );
    }

    /**
     * 骨架修复历史 客户端v1.1.1接口用
     * @return array
     */
    public function frame_history_v100() {
        return array(
            '0' => array('label' => '左A柱外侧', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_a_bar'])),
            '1' => array('label' => '左B柱外侧', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_b_bar'])),
            '2' => array('label' => '左C柱外侧', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_c_bar'])),
            '3' => array('label' => '右C柱外侧', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_c_bar'])),
            '4' => array('label' => '右B柱外侧', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_b_bar'])),
            '5' => array('label' => '右A柱外侧', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_a_bar'])),
            '6' => array('label' => '机盖内缘', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['cover_inner_edge'])),
        );
    }

    /**
     * 统计损伤图片
     * @param string $column 字段 (visible_type,dm_degree)
     * @return Array
     * */
    public function get_dm_pic_stat($column = 'visible_type') {
        $res = M('car_damage_pics')->alias('cdp')->field("COUNT(*) AS cnt ,cd." . $column)
                        ->join(C('DB_PREFIX') . "car_damage cd ON cd.dm_id=cdp.dm_id")
                        ->where("cd.car_id='" . $this->car_id . "' " . ($column == 'dm_degree' ? 'AND cd.dm_degree>1' : ''))->group("cd." . $column)->select();
        $ret = array();
        foreach ($res as $v) {
            $ret[$v[$column]] = $v['cnt'];
        }
        return $ret;
    }

    /**
     * 统计损伤
     * @param string $column 字段
     * @return Array
     */
    public function get_dm_stat($column = 'visible_type') {
        $res = M('car_damage_pics')->field('COUNT(*) AS cnt,' . $column)->where("car_id='" . $this->car_id . "'")->group($column)->select();
        $ret = array();
        foreach ($res as $v) {
            $ret[$v[$column]] = $v['cnt'];
        }
        return $ret;
    }

    /**
     * 获取损伤图片
     * @param int $val 值
     * @param int $limit 返回限制
     * @param int $type 类型
     * @return Array
     */
    public  function get_dm_pic($val = '2', $limit = '4', $type = 'degree') {
        $cond = "cd.car_id='" . $this->car_id . "'";
        if ($type == 'degree') {
            $cond .= " and cdp.dm_degree='" . $val . "'";
        } elseif ($type == 'visible') {
            $cond .= " and cdp.visible_type='" . $val . "'";
        } else {
            return '';
        }
       /* return M('car_damage')->alias('cd')->field('cd.*,cdp.url')->join("LEFT JOIN ".C('DB_PREFIX') . "car_damage_pics cdp ON(cd.dm_id=cdp.dm_id)")
                        ->where($cond)->limit(0, $limit)->select();*/
        return M('car_damage')->alias('cd')->field('cd.dm_id,cd.car_id,cd.position,cd.cus_pos_name,cdp.visible_type,cdp.dm_degree,cdp.describe,cdp.url')->join(C('DB_PREFIX') . "car_damage_pics cdp ON(cd.dm_id=cdp.dm_id)")
            ->where($cond)->limit(0, $limit)->select();
    }

    /**
     *  获取所有损伤图片(jquery 页面调用 些方法）
     * @return json
     */
    public function get_all_pics($flag=false,$type='light') {
        if(!$flag){
            $type = I('get.type', 'light');
        }
        $visible = $this->get_dm_stat(); //可见损伤统计
        $degree = $this->get_dm_stat('dm_degree'); //损伤程度统计

        $json = '';

        switch ($type) {
            case 'light':
                $light_all_pics = $this->get_dm_pic('2', $degree['2'] * 10); //所有轻微损伤图片
                $json = json_encode($light_all_pics,JSON_UNESCAPED_UNICODE);
                break;
            case 'normal':
                $normal_all_pics = $this->get_dm_pic('3', $degree['3'] * 10); //所有一般损伤图片
                $json = json_encode($normal_all_pics,JSON_UNESCAPED_UNICODE);
                break;
            case 'important':
                $important_all_pics = $this->get_dm_pic('4', $degree['4'] * 10); //所有重要损伤图片
                $json = json_encode($important_all_pics,JSON_UNESCAPED_UNICODE);
                break;
            case 'frame':
                $frame_all_pics = $this->get_dm_pic('3', $visible['3'] * 10, 'visible'); //所有骨架损伤图片
                $json = json_encode($frame_all_pics,JSON_UNESCAPED_UNICODE);
                break;
            case 'outside':
                $outside_all_pics = $this->get_dm_pic('2', $visible['2'] * 10, 'visible'); //所有外饰损伤图片
                $json = json_encode($outside_all_pics,JSON_UNESCAPED_UNICODE);
                break;
            case 'inside':
                $inside_all_pics = $this->get_dm_pic('1', $visible['1'] * 10, 'visible'); //所有内饰损伤图片
                $json = json_encode($inside_all_pics,JSON_UNESCAPED_UNICODE);
                break;
        }
        if($flag){
            return $json;
        }else{
            echo $json;
        }
        exit;
    }


    /**
     * 转换以逗号分隔的数字为相应的配置文件中的汉字说明
     * @param $data_src string 数据源 (例：$this->report['perform']['starter'])
     * @param $key string 键名（即car_config.php中的键名 (例：starter（启动机）
     * @return string
     */
    private function get_the_word($key, $data_src) {
        $tmp = explode(',', $data_src);
        $str = '';
        foreach ($tmp as $v) {
            $str .= C(strtoupper($key) . '.' . $v) . '，';
        }
        $str = rtrim($str, '，');
        return $str;
    }

    /**
     * 获取可见损伤点样式
     * @param string $type (frame,outside,inside)
     */
    public function get_dm_point_class() {
        $pos = array();
        $res = $this->car->get_dm_pos('car_damage');
        foreach ($res as $k => $v) {
            if ($v['position'] >= 1 and $v['position'] <= 20) {//内饰
                $i = $v['position']; //2 轻微损伤 3 一般损伤 4 重要损伤
                $pos['inside'][$v['position']] = ($v['dm_degree'] == 2 ? $i . '_' . $i . '_' . $i : ($v['dm_degree'] == 3 ? $i . '_' . $i : ($v['dm_degree'] == 4 ? $i : '')));
            } elseif ($v['position'] >= 22 and $v['position'] <= 69) {//外观
                $i = $v['position'] - 21;
                $pos['outside'][$v['position']] = ($v['dm_degree'] == 2 ? $i . '_' . $i . '_' . $i : ($v['dm_degree'] == 3 ? $i . '_' . $i : ($v['dm_degree'] == 4 ? $i : '')));
            } elseif ($v['position'] >= 71 and $v['position'] <= 93) {//骨架
                $i = $v['position'] - 70;
                $pos['frame'][$v['position']] = ($v['dm_degree'] == 2 ? $i . '_' . $i . '_' . $i : ($v['dm_degree'] == 3 ? $i . '_' . $i : ($v['dm_degree'] == 4 ? $i : '')));
            }
            if ($v['position'] == '96') {//骨架，后围板
                $pos['frame'][$v['position']] = ($v['dm_degree'] == 2 ? '24_24_24' : ($v['dm_degree'] == 3 ? '24_24' : ($v['dm_degree'] == 4 ? '24' : '')));
            }
        }
        return $pos;
    }

    /**
     * 11个覆盖件与7个骨架位置
     *  Car/report_history.html
     *  <div class="xiufuBg{$pos}" style="display:block"></div>
     *  例：左前翼子板 重新喷漆 <div class="xiufuBg1" style="display:block"></div>
     *  例：左前翼子板 重新喷漆且有腻子 <div class="xiufuBg_1" style="display:block"></div>
     * @param string $type 类型（appear 外观 ,frame 骨架
     *  @return Array
     */
    public function get_pos_class($type = 'appear') {
        $pos = array();
        $res = $this->car->detail('car_repair_history');
        //外观覆盖件
        foreach ($res as $k => $v) {
            switch ($k) {
                case 'l_f_wing_board':
                    $pos[$k] = ($v == 2 ? '1' : ($v == 3 ? '_1' : ''));
                    break;
                case 'l_f_door':
                    $pos[$k] = ($v == 2 ? '2' : ($v == 3 ? '_2' : ''));
                    break;
                case 'l_b_door':
                    $pos[$k] = ($v == 2 ? '4' : ($v == 3 ? '_4' : ''));
                    break;
                case 'l_b_wing_board':
                    $pos[$k] = ($v == 2 ? '3' : ($v == 3 ? '_3' : ''));
                    break;
                case 'trunk_cover':
                    $pos[$k] = ($v == 2 ? '10' : ($v == 3 ? '_10' : ''));
                    break;
                case 'r_b_wing_board':
                    $pos[$k] = ($v == 2 ? '7' : ($v == 3 ? '_7' : ''));
                    break;
                case 'r_b_door':
                    $pos[$k] = ($v == 2 ? '8' : ($v == 3 ? '_8' : ''));
                    break;
                case 'r_f_door':
                    $pos[$k] = ($v == 2 ? '6' : ($v == 3 ? '_6' : ''));
                    break;
                case 'r_f_wing_board':
                    $pos[$k] = ($v == 2 ? '5' : ($v == 3 ? '_5' : ''));
                    break;
                case 'engine_cover':
                    $pos[$k] = ($v == 2 ? '9' : ($v == 3 ? '_9' : ''));
                    break;
                case 'car_top':
                    $pos[$k] = ($v == 2 ? '11' : ($v == 3 ? '_11' : ''));
                    break;
                default:
                    break;
            }
        }
        if ($type == 'appear') {
            return $pos;
        }
        //骨架
        $pos = array();
        foreach ($res as $k => $v) {
            switch ($k) {
                case 'l_a_bar':
                    $pos[$k] = ($v == 2 ? '1' : ($v == 3 ? '_1' : ''));
                    break;
                case 'l_b_bar':
                    $pos[$k] = ($v == 2 ? '2' : ($v == 3 ? '_2' : ''));
                    break;
                case 'l_c_bar':
                    $pos[$k] = ($v == 2 ? '3' : ($v == 3 ? '_3' : ''));
                    break;
                case 'r_c_bar':
                    $pos[$k] = ($v == 2 ? '4' : ($v == 3 ? '_4' : ''));
                    break;
                case 'r_b_bar':
                    $pos[$k] = ($v == 2 ? '5' : ($v == 3 ? '_5' : ''));
                    break;
                case 'r_a_bar':
                    $pos[$k] = ($v == 2 ? '6' : ($v == 3 ? '_6' : ''));
                    break;
                case 'cover_inner_edge':
                    $pos[$k] = ($v == 2 ? '7' : ($v == 3 ? '_7' : ''));
                    break;
                default:
                    break;
            }
        }
        return $pos;
    }

    /**
     * //获取部位损伤描述
     */
    public function get_dm_describe() {
        $desc = array();
        foreach ($this->report['damage'] as $k => $value) {
            foreach($value['urls'] as $v){
                switch ($v['visible_type']) {
                    case '3':
                        $desc['frame'][$k] = $value;
                        $desc['frame'][$k]['position_cn'] = $value['cus_pos_name']?$value['cus_pos_name']:C('VISIBLE_DAMAGE_POSITION.' . $value['position']);
                        break;
                    case '2':
                        $desc['outside'][$k] = $value;
                        $desc['outside'][$k]['position_cn'] = $value['cus_pos_name']?$value['cus_pos_name']:C('VISIBLE_DAMAGE_POSITION.' . $value['position']);
                        break;
                    case '1':
                        $desc['inside'][$k] = $value;
                        $desc['inside'][$k]['position_cn'] = $value['cus_pos_name']?$value['cus_pos_name']:C('VISIBLE_DAMAGE_POSITION.' . $value['position']);
                        break;
                }
            }
        }
        return $desc;
    }

    /**
     * 车辆基本照片-小图
     */
    public function base_pics($flag=false) {
        $base_pics = $this->report['pictures'];
        $arr = array();
        foreach ($base_pics as $k => $v) {
            if (in_array($v['pic_key'], array('p_vin', 'p_travel_license', 'p_nameplate'))) {//报告不展示 车驾号，车辆铭牌，行驶证
                continue;
            }
            if($flag){
                $v['pic_url'] = $v['pic_url'];
            }else{
                $v['pic_url'] = $v['pic_url'].'@285w_190h_4e';
            }
            $arr[$k] = $v;
            if ($k >= 11) {
                break;
            }
        }
        return $arr;
    }

    /**
     * json格式化输出图片数据
     * @param string $i
     * @return string
     */
    public function get_base_pics_json($i = '') {
        $base_pics = $this->report['pictures'];
        $arr = array();
        foreach ($base_pics as $k => $v) {
            $arr['pic' . $i][$k]['img'] = $v['pic_url'];
            $arr['pic' . $i][$k]['text'] = $v['pic_desc'];
        }
        return json_encode($arr);
    }

    /**
     * app输出基本图片数据
     * @return string
     */
    public function get_base_pics_app() {
        $condition['car_id'] = $this->car_id;
        $condition['pic_key'] = array('NOT IN', array('p_nameplate', 'p_travel_license', 'p_vin'));
        $base_pics = M('car_pictures')->where($condition)->select();
        $arr = array();
        foreach ($base_pics as $k => $v) {
            $arr[$k]['name'] = $v['pic_desc'];
            $arr[$k]['url'] = get_content_cdnurl().$v['pic_url'];
        }
        return $arr;
    }

    /**
     * 格式化输出损伤图片(接口)
     */
    public function dm_pic_to_json() {
        $visible = $this->get_dm_stat(); //可见损伤统计
        $degree = $this->get_dm_stat('dm_degree'); //损伤程度统计
        //所有轻微损伤图片
        $arr = array();
        $light_all_pics = $this->get_dm_pic('2', $degree['2'] * 10);
        $arr['slight'] = array();
        if ($light_all_pics) {
            foreach ($light_all_pics as $k => $v) {
                $arr['slight'][$k]['text'] = $v['cus_pos_name'] . ' ' . $v['describe'];
                $arr['slight'][$k]['img'] = $v['url'];
            }
        }
        //所有一般损伤图片
        $normal_all_pics = $this->get_dm_pic('3', $degree['3'] * 10);
        $arr['general'] = array();
        if ($normal_all_pics) {
            foreach ($normal_all_pics as $k => $v) {
                $arr['general'][$k]['text'] = $v['cus_pos_name'] . ' ' . $v['describe'];
                $arr['general'][$k]['img'] = $v['url'];
            }
        }
        //所有重要损伤图片
        $important_all_pics = $this->get_dm_pic('4', $degree['4'] * 10);
        $arr['serious'] = array();
        if ($important_all_pics) {
            foreach ($important_all_pics as $k => $v) {
                $arr['serious'][$k]['text'] = $v['cus_pos_name'] . ' ' . $v['describe'];
                $arr['serious'][$k]['img'] = $v['url'];
            }
        }


        $frame_all_pics = $this->get_dm_pic('3', $visible['3'] * 10, 'visible'); //所有骨架损伤图片
        $arr['skeleton'] = array();
        if ($frame_all_pics) {
            foreach ($frame_all_pics as $k => $v) {
                $arr['skeleton'][$k]['text'] = $v['cus_pos_name'] . ' ' . $v['describe'];
                $arr['skeleton'][$k]['img'] = $v['url'];
            }
        }
        $outside_all_pics = $this->get_dm_pic('2', $visible['2'] * 10, 'visible'); //所有外饰损伤图片
        $arr['appearance'] = array();
        if ($outside_all_pics) {
            foreach ($outside_all_pics as $k => $v) {
                $arr['appearance'][$k]['text'] = $v['cus_pos_name'] . ' ' . $v['describe'];
                $arr['appearance'][$k]['img'] = $v['url'];
            }
        }

        $inside_all_pics = $this->get_dm_pic('1', $visible['1'] * 10, 'visible'); //所有内饰损伤图片
        $arr['interior'] = array();
        if ($inside_all_pics) {
            foreach ($inside_all_pics as $k => $v) {
                $arr['interior'][$k]['text'] = $v['cus_pos_name'] . ' ' . $v['describe'];
                $arr['interior'][$k]['img'] = $v['url'];
            }
        }

        echo json_encode($arr);
        exit;
    }

    //获取渠道来源信息
    private function get_from() {
        $from = M("come_from a")->join('(SELECT from_id,from_name,sort FROM au_come_from WHERE pid=-2 AND is_delete=0) AS b ON a.pid=b.from_id')
                ->field("a.from_id,a.from_name,b.from_name AS pname,b.from_id AS pid,b.sort AS psort")
                ->where("a.pid!=-2 AND a.is_delete=0")
                ->order("psort ASC,pid DESC")
                ->select();
        foreach ($from as $key => $val) {
            $show_from[$val['from_id']] = $val['pname'] . '-' . $val['from_name'];
        }
        return $show_from;
    }

    /**
     * 损伤部位 外观、内饰、骨架
     */
   public function get_dm_type($type = '') {
       $res = M('car_damage')->alias('cd')->field('cd.dm_id,cd.position,cd.cus_pos_name,cdp.dm_degree,cdp.describe')
            ->join(C('DB_PREFIX') . "car_damage_pics cdp ON(cd.dm_id=cdp.dm_id)")
            ->where(array('cd.car_id' => $this->car_id, 'visible_type' => $type))
            ->group('cd.position')
            ->select();

        $list = array();
        foreach ($res as $k => $r) {
            $list[$k]['dm_id'] = $r['dm_id'];
            $list[$k]['position'] = (C('VISIBLE_DAMAGE_POSITION')[$r['position']]);
            $list[$k]['dm_degree'] = $r['dm_degree'];
            $list[$k]['cus_pos_name'] = $r['cus_pos_name'];
            $list[$k]['describe'] = $r['describe'];
        }
        return $list;
    }

    /**
     * 获取可见损伤位置图片列表
     * @param type $dm_id
     * @return type
     */
    public function get_dm_position_pics($dm_id = '') {
        /*$position = M('car_damage')->field('position,describe,cus_pos_name')
                ->where(array('dm_id' => $dm_id))
                ->find();*/
        $position = M('car_damage')->alias('cd')->field('cd.position,cd.cus_pos_name,cdp.url,cdp.describe')->join(C('DB_PREFIX') . "car_damage_pics cdp ON(cd.dm_id=cdp.dm_id)")
            ->where(array('cd.dm_id' => $dm_id))->select();
        //$position_name = $position['cus_pos_name']?$position['cus_pos_name']:(C('VISIBLE_DAMAGE_POSITION')[$position['position']]);
        //$describe = $position['describe'];
        /*$position_pics = M('car_damage_pics')->field('url,describe')
                ->where(array('dm_id' => $dm_id))
                ->select();*/
        $dm_position_pics = array();
        $params = array();
        if ($position) {
            foreach ($position as $k => $p) {
                $position_name = $position[$k]['cus_pos_name']?$position[$k]['cus_pos_name']:(C('VISIBLE_DAMAGE_POSITION')[$position[$k]['position']]);
                $dm_position_pics[$k]['name'] = $position_name;
                //$dm_position_pics[$k]['des'] = $describe;
                $dm_position_pics[$k]['des'] = $p['describe'];
                $dm_position_pics[$k]['img'] = $p['url'];
            }
        }else{
            $dm_position_pics[0]['name'] = '';
            $dm_position_pics[0]['des'] = '';
            $dm_position_pics[0]['img'] = '';
        }

        $params = array(
            'pic_count' => count($dm_position_pics),
            'position' => $position[0]['position'],
            'pictures' => $dm_position_pics
        );
        return $params;
    }

    /**
     * 损伤程度图片统计 - 轻微、一般、重要
     */
    public function get_dm_degree_pics() {
        $degree = $this->get_dm_stat('dm_degree'); //损伤程度统计
        //所有轻微损伤图片
        $arr = array();
        $light_all_pics = $this->get_dm_pic('2', $degree['2'] * 10);
        $arr['slight'] = array();
        if ($light_all_pics) {
            foreach ($light_all_pics as $k => $v) {
                $position_name = $v['cus_pos_name']?$v['cus_pos_name']:(C('VISIBLE_DAMAGE_POSITION')[$v['position']]);
                $arr['slight'][$k]['name'] = $position_name;
                $arr['slight'][$k]['des'] = $v['describe'];
                $arr['slight'][$k]['img'] = $v['url'];
            }
        }
        //所有一般损伤图片
        $normal_all_pics = $this->get_dm_pic('3', $degree['3'] * 10);
        $arr['general'] = array();
        if ($normal_all_pics) {
            foreach ($normal_all_pics as $k => $v) {
                $position_name = $v['cus_pos_name']?$v['cus_pos_name']:(C('VISIBLE_DAMAGE_POSITION')[$v['position']]);
                $arr['general'][$k]['name'] = $position_name;
                $arr['general'][$k]['des'] = $v['describe'];
                $arr['general'][$k]['img'] = $v['url'];
            }
        }
        //所有重要损伤图片
        $important_all_pics = $this->get_dm_pic('4', $degree['4'] * 10);
        $arr['serious'] = array();
        if ($important_all_pics) {
            foreach ($important_all_pics as $k => $v) {
                $position_name = $v['cus_pos_name']?$v['cus_pos_name']:(C('VISIBLE_DAMAGE_POSITION')[$v['position']]);
                $arr['serious'][$k]['name'] = $position_name;
                $arr['serious'][$k]['des'] = $v['describe'];
                $arr['serious'][$k]['img'] = $v['url'];
            }
        }
        return $arr;
    }

}
